package Test;
/*
算出以每个字母或者每两个字母为中心的回文字符串长度，然后进行更新
 */
public class 最长回文字符串 {
    static String res="";
    public static String longestPalindrome(String s) {
        for(int i=0;i<s.length();i++){
            String s1=leng(s,i,i+1);
            String s2=leng(s,i,i);
            res=res.length()>s1.length()?res:s1;
            res=res.length()>s2.length()?res:s2;

        }
        return res;
    }

    static String leng(String s,int i,int j){
        while (i>=0&&j<s.length()&&s.charAt(i)==s.charAt(j)){
            i--;
            j++;
        }
        return s.substring(i+1,j);
    }

    public static void main(String[] args) {
        System.out.println("longestPalindrome(\"aaaaaaabbbbbbbbbaaaaaaaa\") = " + longestPalindrome("aaaaaaabbbbbbbbbaaaaaaaa"));
    }
}
